<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <RouterLink to="/">
          <div class="title-bar">
            <img class="logo" src="../../assets/logo.png" alt="logo"/>
            <div class="title">钰玟云图库</div>
          </div>
        </RouterLink>


      </a-col>
      <a-col flex="auto">
        <a-menu v-model:selectedKeys="current" mode="horizontal" :items="items"
                @click="doMenuClick"
        />
      </a-col>
      <a-col flex="120px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
<!--下拉框  注销按钮-->
            <a-dropdown>
              <ASpace>
                <a-avatar :src="loginUserStore.loginUser.userAvatar">
                  {{loginUserStore.loginUser.userName ??'无名'}}
                </a-avatar>
              </ASpace>
              <template #overlay>
                <a-menu>
                  <a-menu-item>
                    <router-link to="/my_space">
                      <UserOutlined />
                      我的空间
                    </router-link>
                  </a-menu-item>
                  <a-menu-item @click="doLogout">
                    <LogoutOutlined/>
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
            {{loginUserStore.loginUser.userName ??'无名'}}
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { computed, h, ref } from 'vue'
// 引入相关 iconfont
import { UserOutlined,HomeOutlined,LogoutOutlined } from '@ant-design/icons-vue'
import { type MenuProps, message } from 'ant-design-vue'

// 引入userLogin pinia
import { useLoginUserStore } from '@/stores/user'
const loginUserStore = useLoginUserStore()

// 引入vue路由
import {useRouter} from "vue-router"
import { userLogoutUsingPost } from '@/api/userController'
import checkAccess from '@/utils/access/checkAccess'
//当前选中的导航栏选项
const current = ref<string[]>([])
//加载路由
const router = useRouter()

// 路由跳转事件
const doMenuClick = ({key}:{key:string}) => {
  router.push(
    {
      path:key,
    }
  )
}
//解决高亮问题  更新current
router.afterEach((to,from,next)=>{
  current.value = [to.path]
})

// 退出登录
const doLogout = async () => {
  const res = await userLogoutUsingPost()
  if(res.data.code === 0 ){
    loginUserStore.setLoginUser({
      userName:'未登录',
    })
    message.success('退出登录成功')
    await router.push('/user/login')
  }else{
    message.error('退出登录失败'+res.data.msg)
  }
}


// 相关路由规则
// const originItems = ref<MenuProps['items']>([
//   {
//     key: '/',
//     icon: () => h(HomeOutlined),
//     label: '主页',
//     title: '主页',
//   },
//   {
//     key: '/admin/userManage',
//     label: '用户管理',
//     title: '用户管理'
//   },
//   {
//     key: 'others',
//     label: h('a',{href:'https://lyf1210.top',target:"_blank"},'个人博客'),
//     title: '个人博客',
//   },
// ])
// const items = computed<MenuProps['items']>(() => filterMenus(originItems))


//路由转化为菜单项
const menuToRouteItem = (item:any)=>{
  const isHome = item.path === '/'
  return {
    key:item.path,
    label:item.name,
    title:item.name,
    icon: isHome ? h(item.meta?.icon ?? HomeOutlined) : undefined, //仅有首页展示icon
  }
}
// 根据权限筛选出来对应的菜单项
const originItems = computed(()=>{
  return router.getRoutes()
    .filter((item)=>{
      if(item.meta?.hideInMenu){
        //需要 隐藏
        return false
      }
      //根据权限过滤菜单
      return checkAccess(loginUserStore.loginUser,item.meta?.access as string )
    })
    .map(menuToRouteItem)//转化为菜单格式
})
//将主页放置到首个
// 找到主页的索引
const homeIndex = originItems.value.findIndex(item => item.key === '/');
if (homeIndex !== -1) {
  // 取出主页
  const homeItem =originItems.value.splice(homeIndex, 1)[0];
  // 将主页插入到数组的开头
  originItems.value.unshift(homeItem);
}
const items = ref<MenuProps['items']>(originItems)

</script>

<style scoped>
.title-bar{
  display: flex;
  align-items: center;
}
.title{
  color:black;
  font-size: 18px;
  margin-left: 16px;
}
.logo{
  height: 48px;
}

</style>
